//
//  SubViewController.m
//  AVFloatView
//
//  Created by hulinhu on 2020/10/22.
//

#import "SubViewController.h"
#import <AVKit/AVKit.h>
#import "ThirdViewController.h"
#import "PiPViewController.h"

#import "PicturePlayView.h"


@interface SubViewController ()<AVPictureInPictureControllerDelegate>

@property (nonatomic, strong) PiPViewController *pipController;
@property (nonatomic, strong) UIButton *changeButton;
@property (nonatomic, strong) PicturePlayView *playView;


@end

@implementation SubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    
    
    //  返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton .frame = CGRectMake(self.view.frame.size.width - 60, 30, 40, 40);
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    
    //  返回按钮
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    nextButton .frame = CGRectMake(self.view.frame.size.width - 90, 80, 70, 40);
    [nextButton setTitle:@"下一页" forState:UIControlStateNormal];
    [nextButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];
    

//    PiPViewController *pipController = [[PiPViewController alloc] init];
//    pipController.view.frame = CGRectMake(0, 160, self.view.frame.size.width, 400);
//    [pipController createPlayerView:pipController.view.bounds];
//    [self.view addSubview:pipController.view];
//    [self addChildViewController:pipController];
//    self.pipController = pipController;
    
    
    PicturePlayView *playView = [[PicturePlayView alloc] initWithFrame:CGRectMake(0, 160, self.view.frame.size.width, 400)];
    [self.view addSubview:playView];
    self.playView = playView;
    
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.pipController startPlayer];
    [self.playView startPlayer];
}


-(void)backBtnClick:(UIButton *)sender {
    
    [self.pipController stopPlay];
    [self.playView stopPlay];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)nextBtnClick:(UIButton *)sender {
    ThirdViewController *subVC = [[ThirdViewController alloc] init];
    [self.navigationController pushViewController:subVC animated:YES];
}


#pragma mark
#pragma mark - delegate

- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"将要开始PictureInPicture的代理方法");
}

- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"已经开始PictureInPicture的代理方法");
}

- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error {
    NSLog(@"启动PictureInPicture失败的代理方法");
}

- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"将要停止PictureInPicture的代理方法");
}

- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"已经停止PictureInPicture的代理方法");
}

- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler {
    //此方法执行在pictureInPictureControllerWillStopPictureInPicture代理方法之后，在pictureInPictureControllerDidStopPictureInPicture执行之前。 但是点击“X”移除画中画时，不执行此方法。
    NSLog(@"PictureInPicture停止之前恢复用户界面");
}




@end
